﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace DotNETX.Utility.Async
{

    public class BackgroundWorkerUtils
    {
        /// <summary>
        /// 新建BackgroundWorker
        /// </summary>
        /// <param name="doWork"></param>
        /// <param name="runWorkCompleted"></param>
        /// <returns></returns>
        public static BackgroundWorker New(DoWorkEventHandler doWork, RunWorkerCompletedEventHandler runWorkCompleted)
        {
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += doWork;
            bw.RunWorkerCompleted += runWorkCompleted;
            return bw;
        }

        /// <summary>
        /// BackgroundWorker
        /// </summary>
        /// <param name="doWork"></param>
        /// <param name="progressChanged"></param>
        /// <param name="runWorkCompleted"></param>
        /// <returns></returns>
        public static BackgroundWorker New(DoWorkEventHandler doWork, ProgressChangedEventHandler progressChanged, RunWorkerCompletedEventHandler runWorkCompleted)
        {
            BackgroundWorker bw = new BackgroundWorker();
            bw.WorkerReportsProgress = true;
            bw.DoWork += doWork;
            bw.ProgressChanged += progressChanged;
            bw.RunWorkerCompleted += runWorkCompleted;
            return bw;
        }

        /// <summary>
        /// 使用BackgroundWorker异步调用
        /// </summary>
        /// <param name="doWork"></param>
        /// <param name="runWorkCompleted"></param>
        /// <param name="userState"></param>
        public static void DoAsyncWork(DoWorkEventHandler doWork, RunWorkerCompletedEventHandler runWorkCompleted, object userState = null)
        {
            BackgroundWorker bw = New(doWork, runWorkCompleted);
            bw.RunWorkerAsync(userState);
        }

        /// <summary>
        /// 使用BackgroundWorker异步调用
        /// </summary>
        /// <param name="doWork"></param>
        /// <param name="progressChanged"></param>
        /// <param name="runWorkCompleted"></param>
        /// <param name="userState"></param>
        public static void DoAsyncWork(DoWorkEventHandler doWork, ProgressChangedEventHandler progressChanged, RunWorkerCompletedEventHandler runWorkCompleted, object userState = null)
        {
            BackgroundWorker bw = New(doWork, progressChanged, runWorkCompleted);
            bw.RunWorkerAsync(userState);
        }

        /// <summary>
        /// 异步延时执行
        /// </summary>
        /// <param name="doWork">执行方法体</param>
        /// <param name="millisecond">延时时间（毫秒）</param>
        /// <param name="userState"></param>
        public static void SetTimeout(Action<object> doWork, int millisecond, object userState)
        {
            BackgroundWorker bw = New((s, e) => { System.Threading.Thread.Sleep(millisecond); e.Result = e.Argument; }, (s, e) => { doWork(e.Result); });
            bw.RunWorkerAsync(userState); 
        }
    }
}
